<?php
class admin_helpers_editRow extends Zend_View_Helper_Abstract
{
	/**
	 * @var Engine_Core_Wrapper
	 */
	protected $_entry;

	/**
	 * @var string
	 */
	protected $_markup = '/';

	/**
	 * @var array
	 */
	protected $_row = array();

	/**
	 * @param Engine_Core $entry
	 * @param array $fields
	 * @param string $markup
	 * @return string
	 */
	public function editRow($entry, $fields, $markup = '')
	{
		$this->_entry   = $entry;
		$this->_markup = '/' . $markup;

		foreach($fields as $field) {
			$method = 'draw' . ucfirst($field);

			if (method_exists($this, $method)) {
				$this->_row[$field] = $this->$method($field);
			} else {
				$this->_row[$field] = $this->drawSimple($field);
			}
		}

		return $this->view->partial('templates/editRow' . $this->_markup . '/drawRow.phtml', array('rows' => $this->_row, 'entry' => $this->_entry));
	}

	/**
	 * @param string $field
	 * @return void
	 */
	protected function drawSimple($field)
	{
		if (strpos($field, 'button') === 0) {
			$field = str_replace('button.', '', $field);

			return $this->view->partial('templates/editRow' . $this->_markup . '/button/' . $field . '.phtml', array(
				'entry' => $this->_entry,
				'field' => $field
			));
		}

		return $this->view->partial('templates/editRow' . $this->_markup . '/drawSimple.phtml', array(
			'entry' => $this->_entry,
			'field' => $field
		));
	}

	/**
	 * @param string $field
	 * @return void
	 */
	protected function drawTitle($field)
	{
		$request = Zend_Controller_Front::getInstance()->getRequest();

		$url = $this->view->url(array(
			'controller' => $request->getControllerName(),
			'action' => 'edit',
			'id' => $this->_entry->id
		));

		return $this->view->partial('templates/editRow' . $this->_markup . '/drawTitle.phtml', array(
			'entry' => $this->_entry,
			'field' => $field,
			'url'   => $url
		));
	}
}